package com.comac.print.component.app.command.cmdExe;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson.JSONObject;
import com.comac.print.component.app.command.CommandExe;
import com.comac.print.component.app.command.printSheetExe.FollowSheetExe;
import com.comac.print.component.app.common.PrintCommon;
import com.comac.print.component.app.domain.gateway.print.PrintRecordGateway;
import com.comac.print.component.app.dto.MasterPlateDTO;
import com.comac.print.component.app.gatewayimpl.database.dataobject.PrintRecordDO;
import com.comac.print.component.app.gatewayimpl.database.mapper.PrintRecordMapper;
import com.comac.print.component.app.utils.ZipUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

@Slf4j
@Component
public class FoFollowCmdExe extends CommandExe {

    @Autowired
    private PrintRecordGateway printRecordGateway;
    @Autowired
    private PrintRecordMapper printRecordMapper;

    @Autowired
    private FollowSheetExe followSheetExe;

    @Override
    public Response execute(PrintCommon printCommon) {/*
        FoSourceDTO foSourceDTO = new FoSourceDTO();
        PrintRecordE   printRecord = new PrintRecordE();
        BeanUtil.copyProperties(printCommon,printRecord);
        //        //2.通过接口地址和接口入参获取业务数据
        log.info("PrintRecordCmdExe  query param is {}", JSONObject.toJSONString(printCommon));
        String result = OkHttp3Util.syncPostJson(printCommon.getDataSourceUrl(), printCommon.getInputParameterValue());
        log.info("PrintRecordCmdExe  query result is {}", result);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if(!jsonObject.getBoolean("success")) {
            return SingleResponse.buildFailure("-1", jsonObject.get("message").toString());
        }
        if(!jsonObject.containsKey("data")){
            return SingleResponse.buildFailure("-1", result);
        }
        String dataJson = JSONObject.toJSONString(jsonObject.get("data"));
        foSourceDTO = JSONObject.parseObject(dataJson, FoSourceDTO.class);
        SingleResponse singleResponse = null;
        //3.拿到业务数据组合模板生成pdf
        try {
            singleResponse = followSheetExe.printFollowSheet(foSourceDTO,printCommon.getMpUrl());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(!singleResponse.isSuccess()){
            //printRecord.setFailureReason(singleResponse.getErrMessage());
        }
        Object data = singleResponse.getData();
        if(data != null){
            //printRecord.setPrintUrl(data.toString());
        }
        //printRecord.setStId(printCommon.getStId());
        //传递消费结果的topic的tag
        //printRecord.setTag(TagEnum.follow.name());
        printRecordGateway.create(printRecord);*/
        return SingleResponse.buildSuccess();
    }

    @Override
    public Response executeSync(PrintCommon printCommon) {
        MasterPlateDTO masterPlateDTO = printCommon.getMasterPlateDTO();
        //打印记录
        PrintRecordDO printRecord = new PrintRecordDO();
        BeanUtil.copyProperties(masterPlateDTO,printRecord,"id");

        Date date = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
        printRecord.setOperatorTime(date);
        //记录调用打印接口的数据
        printRecord.setPrintData(ZipUtil.zip2Base64(printCommon.getData().toString()));
        //3.拿到业务数据组合模板生成pdf
        try {
            SingleResponse singleResponse = followSheetExe.printFollowSheet(printCommon);
            printRecord.setIsSuccess(singleResponse.isSuccess());
            printRecord.setPrintFileUrl(((JSONObject) singleResponse.getData()).getString("printSheetOssUrl"));
            printRecordMapper.insert(printRecord);
            return SingleResponse.of(singleResponse.getData());

        } catch (Exception e) {
            log.error("获取printCommon失败", e);
            printRecord.setErrMessage(e.getMessage());
            printRecord.setIsSuccess(false);
            printRecordMapper.insert(printRecord);
            return SingleResponse.buildFailure("500", "打印失败:" + e.getMessage());
        }
    }
}
